<?php
//--Acan帮助

class Con_2dfire extends Acan{
	
	public $defaultAct='class';
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		$this->pgsql = Afun_db('pgsql',array(
			'host'=>'127.0.0.1',
			'user'=>'au',
			'pass'=>'s100200',
			'name'=>'rerp4',
			'port'=>'5432',
			'charset'=>'utf8'
		));
		$this->db = $this->pgsql->adbs;
	}
	function Act_instance(){
			$enum['orderkind'] = array('1'=>'正常开单','2'=>'预订开单','3'=>'排队开单','3'=>'外卖开单');
			$enum['status'] = array('1'=>'正常','2'=>'并单','3'=>'撤消','3'=>'结账');
			$enum['ishide'] = array('0'=>'否','1'=>'是');
			$enum['isvalid'] = array('0'=>'无效','1'=>'有效');
			$rs = $this->pgsql->findAll("SELECT * FROM userinfo");
			foreach ($rs['data'] as $k => $v) {
				$enum['opuserid'][$v['id']] = $v['name'];
			}
			$enum['ratiooperatorid'] = $enum['workerid'] = $enum['opuserid'];
		$columns = array(
			'orderid'=>'订单ID',
			'kind'=>'1:普通菜;2:套菜;3:自定义菜;4:自定义套菜',
			'parentid'=>'套菜父菜ID',
			'pricemode'=>'1:固定价;2:浮动价',
			'name'=>'菜名',
			'menuid'=>'菜品ID',
			'bookmenuid'=>'菜谱菜肴ID',
			'kindmenuid'=>'菜类ID',
			'kindbookmenuid'=>'菜谱菜类ID',
			'makeid'=>'做法ID',
			'makename'=>'做法',
			'makeprice'=>'做法调价',
			'makepricemode'=>'1,一次性加价;2,按点菜单位加价;3,按结账单位加价',
			'taste'=>'口味说明',
			'specdetailname'=>'规格名',
			'specdetailid'=>'规格明细ID',
			'specpricemode'=>'1/使用PriceScale字段按比例计算价格...',
			'specdetailprice'=>'规格调价',
			'num'=>'点菜数量',
			'accountnum'=>'结账数量',
			'unit'=>'点菜单位',
			'accountunit'=>'结账单位',
			'iswait'=>'是否待菜',
			'memo'=>'说明',
			'isbuynumberchanged'=>'是否修改过点菜数量',
			'originalprice'=>'AMOUNT',
			'price'=>'单价',
			'memberprice'=>'会员价',
			'fee'=>'金额',
			'isratio'=>'可否打折',
			'ratio'=>'折扣率',
			'ratiofee'=>'折后金额',
			'ratiooperatorid'=>'打折操作人',
			'ratiocause'=>'打折原因',
			'prodplanid'=>'出品方案ID',
			'status'=>'状态：...',
			'childid'=>'套菜可换菜原始ID',
			'workerid'=>'点菜人',
			'opuserid'=>'操作人',
			'isbackauth'=>'退菜是否需要权限',
			'isvalid'=>'是否有效',
			'createtime'=>'记录生成时间',
			'optime'=>'操作时间',
			'lastver'=>'版本号');
		$where = " WHERE i.status=?";
		if($_GET['startdate']){
			$where .= ' AND ';
		}
		//$left = ' LEFT JOIN pay p ON i.id=p.totalpayid ';
		$order = ' ORDER BY i.optime DESC';
		$rs = $this->pgsql->findAll("SELECT i.* FROM instance i".$left.$where.$order,2);
		include $this->Atpl();
	}
	function Act_order(){
		$enum = Afun_cache_get('enum:totalpay');
		if($enum){
			$enum['orderkind'] = array('1'=>'正常开单','2'=>'预订开单','3'=>'排队开单','3'=>'外卖开单');
			$enum['status'] = array('1'=>'正常','2'=>'并单','3'=>'撤消','3'=>'结账');
			$enum['ishide'] = array('0'=>'否','1'=>'是');
			$enum['isvalid'] = array('0'=>'无效','1'=>'有效');
			$rs = $this->pgsql->findAll("SELECT * FROM userinfo");
			foreach ($rs['data'] as $k => $v) {
				$enum['workerid'][$v['id']] = $v['name'];
			}
			$enum['opuserid'] = $enum['workerid'];
			Afun_cache_set('enum:totalpay',$enum,600);
		}
		$columns = array('orderkind'=>'开单',
			'currdate'=>'营业日期',
			'code'=>'单号',
			'innercode'=>'格式：营业日期（YYYYMMDD）+流水号（4位）',
			'areaid'=>'区域ID',
			'seatid'=>'座位ID',
			'menutimeid'=>'菜肴分时段Id',
			'bookid'=>'菜谱ID',
			'reserveid'=>'关联预订ID',
			'totalpayid'=>'账单ID',
			'workerid'=>'服务员ID',
			'peoplecount'=>'就餐人数',
			'opentime'=>'开单时间',
			'endtime'=>'结单时间',
			'status'=>'状态',
			'feeplanid'=>'附加费用方案ID',
			'memo'=>'整单说明',
			'iswait'=>'待叫标志',
			'isprint'=>'0/不入厨（补单）...',
			'opuserid'=>'操作员',
			'ishide'=>'是否隐藏',
			'isvalid'=>'是否有效',
			'createtime'=>'记录生成时间',
			'optime'=>'操作时间',
			'lastver'=>'版本号');
		$where = " WHERE ?";
		if($_GET['startdate']){
			$where .= ' AND ';
		}
		//$left = ' LEFT JOIN pay p ON t.id=p.totalpayid ';
		$order = ' ORDER BY o.optime DESC';
		$rs = $this->pgsql->findAll("SELECT o.* FROM orderinfo o".$left.$where.$order,1);
		include $this->Atpl();
	}
	
	function Act_pay(){
		$enum = Afun_cache_get('enum:totalpay');
		if(!$enum){
			$enum['status'] = array('1'=>'正常','2'=>'结账完毕','3'=>'反结账');
			$enum['overstatus'] = array('1'=>'未交班','2'=>'已交班');
			$enum['isvalid'] = array('0'=>'无效','1'=>'有效');
			$enum['ishide'] = array('0'=>'无','1'=>'有');
			$rs = $this->pgsql->findAll("SELECT * FROM kindpay");
			foreach ($rs['data'] as $k => $v) {
				$enum['kindpayid'][$v['id']] = $v['name'];
			}
			$rs = $this->pgsql->findAll("SELECT * FROM userinfo");
			foreach ($rs['data'] as $k => $v) {
				$enum['operator'][$v['id']] = $v['name'];
			}
			$enum['opuserid'] = $enum['operator'];
			$rs = $this->pgsql->findAll("SELECT * FROM discountplan");
			foreach ($rs['data'] as $k => $v) {
				$enum['discountplanid'][$v['id']] = $v['name'];
			}
			Afun_cache_set('enum:totalpay',$enum,600);
		}
	$columns = array('currdate'=>'发生日期',
	'sourceamount'=>'原始费用',
	'discountamount'=>'折后费用',
	'resultamount'=>'应付总额',
	'recieveamount'=>'实收总额',
	'discountplanid'=>'采用的打折方案',
	'operator'=>'收银员',
	'operatedate'=>'结账时间',
	'cardid'=>'会员卡ID',
	'card'=>'会员卡号',
	'cardentityid'=>'会员卡系统Id',
	'isfullratio'=>'是否整体打折',
	'ratio'=>'折扣率',
	'isminconsumeratio'=>'最低消费是否打折',
	'isservicefeeratio'=>'服务费是否打折',
	'invoice'=>'0表示未开发票',
	'printnum1'=>'客户联打印次数',
	'printnum2'=>'财务联打印次数',
	'status'=>'状态',
	'overstatus'=>'交班',
	'ishide'=>'查询条件',
	'isvalid'=>'是否有效',
	'createtime'=>'记录生成时间',
	'optime'=>'操作时间',
	'lastver'=>'版本号',
	'opuserid'=>'操作人',
	'kindpayid'=>'付款类型',
	//pay
	'fee'=>'实收额',
	'paytime'=>'收银时间',
	'pay'=>'现收金额',
	'charge'=>'找零',
	'cardentityid'=>'会员卡系统Id',
	'isdealed'=>'处理标识(对挂账收款的支持)');
		$where = " WHERE ?";
		if($_GET['startdate']){
			$where .= ' AND ';
		}
		$left = ' LEFT JOIN pay p ON t.id=p.totalpayid ';
		$order = ' ORDER BY t.optime DESC';
		$rs = $this->pgsql->findAll("SELECT t.*,p.* FROM totalpay t".$left.$where.$order,1);
		include $this->Atpl();
	}
	function Act_stat(){
		$this->atitle = '很多人咖啡 营业图表';
		$rs = Afun_cache_get('stat:totalpay');
		if(!$rs){
			echo $startdata = date('Y-m-d',time()-86400*8);
			$where = " WHERE t.currdate>? and t.status=?";
			$group = ' GROUP BY t.currdate ORDER BY currdate ASC';
			$rs = $this->pgsql->findAll("SELECT t.currdate,count(t.recieveamount) as nums,SUM(t.recieveamount) as money FROM totalpay t".$where.$group,$startdata,2);
			Afun_cache_set('stat:totalpay',$rs,3600);
		}
		foreach ($rs['data'] as $k => $v) {
			$drr[] = $v['currdate'];
			$mrr[] = $v['money'];
			$nrr[] = $v['nums'];
		}
		$json_date = json_encode($drr);
		$json_num = json_encode($nrr);
		$json_money = json_encode($mrr);
		$total_num = array_sum($nrr);
		$total_money = array_sum($mrr);
		$avg = round($total_money/$total_num,1);
		$dayavg = round($total_money/7,1);
		//--统计7日内各菜品销量
		$rs = Afun_cache_get('stat:7dayin');
		if(!$rs){
			$where = " WHERE i.optime>? and i.status=?";
			$group = ' GROUP BY i.name,i.kindmenuid ORDER BY i.kindmenuid ASC,num DESC';
			$rs = $this->pgsql->findAll("SELECT i.name,count(i.name) as num FROM instance i".$where.$group,(time()-86400*8)*1000,2);
			Afun_cache_set('stat:7dayin',$rs,3600);
		}
		$day5max = 0;
		foreach ($rs['data'] as $k => $v) {
			if($day5max<$v['num']) $day5max=$v['num'];
			$arr['name'] = $v['name'];
			$arr['value'] = $v['num'];
			$arr['color']='#FF3333';
			$day5data[] = $arr;
		}
		$json_day5data = json_encode($day5data);
		
		include $this->Atpl();
	}
}

?>